package com.my.oschina.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.my.oschina.R;
import com.my.oschina.adapter.TweetFragmentPagerAdapter;
import com.my.oschina.app.AppContext;
import com.my.oschina.common.SimpleTwoTabsListener;
import com.my.oschina.common.UIHelper;
import com.my.oschina.ui.LoginActivity;

/**
 * 侧边栏导航-动弹
 */
public class TweetFragment extends SherlockFragment {

	private ViewPager viewPager;
	private List<Fragment> tFragments = new ArrayList<Fragment>();

	/**
	 * 返回动弹TweetFragment的实例对象
	 * 
	 * @param uid
	 *            用户id
	 * @return TweetFragment实例
	 */
	public static TweetFragment newInstance(int uid) {
		TweetFragment ff = new TweetFragment();
		Bundle bundle = new Bundle();
		bundle.putInt("uid", uid);
		ff.setArguments(bundle);
		return ff;
	}

	private int getArgsUid() {
		return getArguments().getInt("uid", 0);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 显示菜单
		setHasOptionsMenu(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initActionBarAndTab(getSherlockActivity().getSupportActionBar(),
				R.string.tab_tweet_lastest, R.string.tab_tweet_hot,
				R.string.tab_tweet_my, 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.viewpager_layout, container,
				false);
		viewPager = (ViewPager) view;
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		viewPager.setOffscreenPageLimit(3);
		viewPager.setOnPageChangeListener(onPageChangeListener);

		TweetFragmentPagerAdapter adapter = new TweetFragmentPagerAdapter(this,
				viewPager, getChildFragmentManager(), tFragments, getArgsUid());

		viewPager.setAdapter(adapter);
	}

	/**
	 * 初始化actionbar并给viewpager不同页面设置标题
	 * 
	 * @param actionBar
	 * @param firstTab
	 * @param secondTab
	 * @param thirdTab
	 * @param nav
	 */
	public void initActionBarAndTab(ActionBar actionBar, int firstTab,
			int secondTab, int thirdTab, int nav) {

		actionBar.setLogo(R.drawable.slm_action_tweet);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.removeAllTabs();

		SimpleTwoTabsListener tabListener = new SimpleTwoTabsListener(viewPager);
		actionBar.addTab(actionBar.newTab().setText(firstTab)
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText(secondTab)
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText(thirdTab)
				.setTabListener(tabListener));

		if (actionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS)
			actionBar.setSelectedNavigationItem(nav);
	}

	/**
	 * ViewPager.SimpleOnPageChangeListener
	 */
	ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			if (position == 2) {
				AppContext ac = (AppContext) getSherlockActivity()
						.getApplication();
				if (!ac.isLogin()) {
					Intent i = new Intent(getSherlockActivity(),
							LoginActivity.class);
					startActivity(i);
				}
			}
			if (getSherlockActivity().getSupportActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
				getSherlockActivity().getSupportActionBar()
						.setSelectedNavigationItem(position);
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			super.onPageScrollStateChanged(state);
			// 暂不知具体作用
			switch (state) {
			case ViewPager.SCROLL_STATE_SETTLING:
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 返回动弹管理的子Fragment-这里使用了嵌套的Fragment
	 * 
	 * @param type
	 * @return
	 */
	public TweetChildFragment getTweetChildFragment(int type) {
		// 由于多个Tab使用同一个QuestionChildFragment，因此这种获取方式应该是不对的
		TweetChildFragment fragment = ((TweetChildFragment) getChildFragmentManager()
				.findFragmentByTag(TweetChildFragment.class.getName()));
		if (fragment == null) {
			fragment = TweetChildFragment.newInstance(type);
		}
		return fragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.menu_tweet, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// 写动弹
		case R.id.menu_tweet_edit:
			UIHelper.showTweetPub(getSherlockActivity());
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
